<?php
    require_once BASE.'auto_backup/google-api-php-client/src/Google_Client.php';
    require_once BASE.'auto_backup/google-api-php-client/src/contrib/Google_DriveService.php';

    function uploadto_ggdrive($filename,$title='',$folder_id=null)
    {
        
        $client = new Google_Client();
        // Get your credentials from the APIs Console
        $client->setClientId(CLIENT_ID);
        $client->setClientSecret(SECRET_ID);
        $client->setRedirectUri(REDIRECT);
        //Sonhb - Access only create
        $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

        $service = new Google_DriveService($client);

        //request access
        $tok_string=base64_decode(file_get_contents(BASE.'auto_backup/kurtulus_autobackup_config/tok.kurtulus'));
        $tok=json_decode($tok_string);
        //Refresh
        /*
        if(($tok->created)+($tok->expires_in)>time())
        {
        $client->refreshToken($tok->refresh_token);
        $accessToken = $client->getAccessToken();
        $fh = fopen('kurtulus_autobackup_config/tok.kurtulus', 'w');
        fwrite($fh, $accessToken);
        fclose($fh);
        $tok=json_decode($accessToken);
        }
        */
        $client->setAccessToken($tok_string);
        
        //Insert a file
        $file = new Google_DriveFile();
        //$title='Backup at '.date('Y-m-d');
        $title = basename($filename);
        $file->setTitle($title);
        $file->setDescription('K & P backup file at '.date('Y-m-d'));
        $file->setMimeType('application/x-tar');
        $parent = new Google_ParentReference();
        if($folder_id==null)
        $folder_id=FOLDER_ID;
        $parent->setId($folder_id);
        $file->setParents(array($parent));
        //

        $data = file_get_contents($filename);
        $service = new Google_DriveService($client);
        $insertedFile = $service->files->insert($file, array('data' => $data, 'mimeType' => 'application/x-tar'));
        
        $file_info=null;
        $file_info->name=$insertedFile['title'];
        $file_info->link=$insertedFile['alternateLink'];
        $file_info->size=number_format((filesize($filename)) / 1048576, 2) . ' MB';
        if(AUTO_CLEAR)
        {
            unlink($filename);
        }
        return $file_info;

    }
    function create_folder($foldername='')
    {
        $client = new Google_Client();
        $client->setClientId(CLIENT_ID);
        $client->setClientSecret(SECRET_ID);
        $client->setRedirectUri(REDIRECT);
        $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
        $service = new Google_DriveService($client);
        //request access
        $tok_string=base64_decode(file_get_contents(BASE.'auto_backup/kurtulus_autobackup_config/tok.kurtulus'));
        $tok=json_decode($tok_string);
        $client->setAccessToken($tok_string);        
        //Insert a folder
        $file = new Google_DriveFile();
        if($foldername=='')
        $title = date('Y-m-d');
        else
        $title = $foldername;
        $file->setTitle($title);
        $file->setDescription('K & P backup folder at '.date('Y-m-d'));
        $file->setMimeType('application/vnd.google-apps.folder');
        $parent = new Google_ParentReference();
        $parent->setId(FOLDER_ID);
        $file->setParents(array($parent));

        $service = new Google_DriveService($client);
        $insertedFile = $service->files->insert($file);
        
        $file_info=null;
        $file_info->name=$insertedFile['title'];
        $file_info->link=$insertedFile['alternateLink'];
        //return folder id
        return $insertedFile['id'];
    }

?>
